package com.balabala.gym_system.controller.admin;

/**
 * Author:zl
 * Date:2023/5/1 16:50
 */

import com.balabala.gym_system.pojo.dto.BaseDeleteDto;
import com.balabala.gym_system.pojo.dto.RoleQueryDto;
import com.balabala.gym_system.pojo.dto.UpdateRoleMenuDto;
import com.balabala.gym_system.pojo.entity.Role;
import com.balabala.gym_system.pojo.vo.RoleListVo;
import com.balabala.gym_system.service.RoleService;
import com.balabala.result.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by zxd on 2023/3/23
 */
@RequestMapping("/admin/role")
@RestController
@Api(tags = "角色相关接口")
@Slf4j
@CrossOrigin
public class RoleController {

    @Autowired
    private RoleService roleService;


    @ApiOperation(value = "查询所有角色")
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Result findAll() {
        List<Role> roleList = roleService.list();
        return Result.ok(roleList);
    }


    @RequestMapping(value = "/findRoleByAdminId", method = RequestMethod.GET)
    @ApiOperation(value = "根据用户id查询用户角色", notes = "根据用户id查询用户角色", httpMethod = "GET")
    public Result findRoleByAdminId(@RequestParam Long adminId) {
        List<Role> roleList = roleService.findRoleByAdminId(adminId);
        return Result.ok(roleList);
    }

    @ApiOperation(value = "更新角色菜单", notes = "更新角色菜单", httpMethod = "POST")
    @RequestMapping(value = "/updateRoleMenu", method = RequestMethod.POST)
    public Result updateRoleMenuByRoleId(@RequestBody UpdateRoleMenuDto updateRoleMenuDto) {
        roleService.updateRoleMenu(updateRoleMenuDto);
        return Result.ok();
    }
    @ApiOperation(value = "根据id查询角色")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result find(@PathVariable("id") Long id) {
        Role role = roleService.getById(id);
        return Result.ok(role);
    }
    @ApiOperation(value = "添加角色")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody @Validated Role role) {

        boolean result = roleService.save(role);
        return Result.judge(result);
    }

    @ApiOperation(value = "修改角色")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody Role role) {
        boolean result = roleService.updateById(role);
        return Result.judge(result);
    }

    @ApiOperation(value = "删除角色")
    @RequestMapping(value = "/deleteById", method = RequestMethod.GET)
    public Result deleteById(@RequestParam Long id) {
        boolean result = roleService.removeById(id);
        return Result.judge(result);
    }

    @ApiOperation(value = "批量删除角色")
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    public Result deleteByIds(@RequestBody BaseDeleteDto baseDeleteDto) {
        boolean result = roleService.removeByIds(baseDeleteDto.getIds());
        return Result.judge(result);
    }
}